package great.easychat.help.activity

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager.widget.ViewPager
import cn.net.shoot.sharetracesdk.AppData
import cn.net.shoot.sharetracesdk.ShareTrace
import cn.net.shoot.sharetracesdk.ShareTraceInstallListener
import great.easychat.help.BaseActivity
import great.easychat.help.R
import great.easychat.help.adapter.CommonPagerAdapter
import great.easychat.help.fragment.HomeFragment
import great.easychat.help.fragment.MeFragment
import great.easychat.help.util.DebugLog
import great.easychat.help.viewModel.SplashViewModel
import kotlinx.android.synthetic.main.activity_main_new.*
import java.util.*


class MainNewActivity : BaseActivity<SplashViewModel>() {

    private var pageList = ArrayList<Fragment>()
    private lateinit var pageAdapter: CommonPagerAdapter
    override fun observeLiveData() {}
    override fun requestLayoutId(): Int {
        return R.layout.activity_main_new
    }

    override fun isToolBarEnable(): Boolean {
        return false
    }

    override fun setViewData(savedInstanceState: Bundle?): Unit {

        ShareTrace.getInstallTrace(object : ShareTraceInstallListener {
            override fun onInstall(data: AppData) {
                DebugLog.d("appData= " + data.getChannel() + " - " + data.getParamsData())
            }

            override fun onError(code: Int, msg: String) {
                DebugLog.d("Get install trace info error. code=$code,msg=$msg")
            }
        })

        pageList.add(HomeFragment())
        pageList.add(HomeFragment())
        pageList.add(HomeFragment())
        pageList.add(MeFragment())

        mainPager.offscreenPageLimit = pageList.size
        pageAdapter = CommonPagerAdapter(pageList, supportFragmentManager)
        mainPager.adapter = pageAdapter
        mainPager.currentItem = 0

        mainPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                selectTab(position)
            }
        })

        rgMainTab.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.rbHome -> {
                    mainPager.currentItem = 0
                }
                R.id.rbMore -> {
                    mainPager.currentItem = 1
                }
                R.id.rbMessage -> {
                    mainPager.currentItem = 2
                }
                R.id.rbMe -> {
                    mainPager.currentItem = 3
                }
            }
        }

    }

    private fun selectTab(position: Int) {
        when (position) {
            0 -> {
                if (rbHome.isChecked) {
                    return
                }
                rgMainTab.check(R.id.rbHome)
            }

            1 -> {
                if (rbMore.isChecked) {
                    return
                }
                rgMainTab.check(R.id.rbMore)
            }
            2 -> {
                if (rbMessage.isChecked) {
                    return
                }
                rgMainTab.check(R.id.rbMessage)
            }
            3 -> {
                if (rbMe.isChecked) {
                    return
                }
                rgMainTab.check(R.id.rbMe)
            }
        }
    }

    override fun onResume() {
        super.onResume()
//        UpdateHelper.startCheck(this)
    }

}
